using Microsoft.SqlServer.Management.Smo;
using System;
using System.Data.SqlClient;
using System.Globalization;

namespace Microsoft.SqlServer.Tools.PublishWizard.Helpers
{
	internal class Connection : IDisposable
	{
		private static Connection _instance;

		private SqlConnection _sqlConnection;

		private Database _database;

		public static Connection ConnectionInstance
		{
			get
			{
				if (Connection._instance == null)
				{
					Connection._instance = new Connection();
				}
				return Connection._instance;
			}
		}

		public SqlConnection SqlConnection
		{
			get
			{
				return this._sqlConnection;
			}
		}

		public Database Database
		{
			get
			{
				return this._database;
			}
		}

		private Connection()
		{
		}

		public void Init(Database database)
		{
			this._sqlConnection = new SqlConnection(string.Format(CultureInfo.InvariantCulture, "{0};database={1}", new object[]
			{
				database.Parent.ConnectionContext,
				database.Name
			}));
			this._sqlConnection.Open();
			this._database = database;
		}

		public void Dispose()
		{
			if (this._sqlConnection != null)
			{
				this._sqlConnection.Close();
				this._sqlConnection = null;
			}
		}
	}
}
